package com.course.servlet;

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class EncodingFilter implements Filter {

    public void destroy() {

       // TODO Auto-generated method stub

      

    }

    public void doFilter(ServletRequest arg0, ServletResponse arg1,

           FilterChain arg2) throws IOException, ServletException {

       //参数转换，因为我们已经它肯定是Http协议的请求。    

       HttpServletRequest request = (HttpServletRequest)arg0;

       HttpServletResponse response = (HttpServletResponse)arg1;

       //设置request和response使用的编码均为UTF-8。

       request.setCharacterEncoding("UTF-8");

       response.setCharacterEncoding("UTF-8");

       response.setContentType("text/html;charset=UTF-8");

       //设置完成后，交回给服务器。

       arg2.doFilter(arg0, arg1);     

    }

    public void init(FilterConfig arg0) throws ServletException {

       // TODO Auto-generated method stub

      

    }

}
